#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(false), cin.tie(0)
using namespace std;
typedef long long ll;
const int maxn=10000;

namespace ac{
    int dp[maxn][maxn];
    int store[maxn][maxn];
    int n,k;
    void init(){
        memset(dp,0,sizeof(dp));
    }
    int dfs(int x,int y){
        if(dp[x][y]>0)return dp[x][y];
        dp[x][y]=store[x][y];
        for(int i=1;i<=k;i++){//走i步
            if(x-i>=0&&store[x-i][y]>store[x][y]){//向上走i步
                dp[x][y]=max(dp[x][y],dfs(x-i,y)+store[x][y]);
            }
            if(x+i<n&&store[x+i][y]>store[x][y]){//向下走i步
                dp[x][y]=max(dp[x][y],dfs(x+i,y)+store[x][y]);
            }
            if(y-i>=0&&store[x][y-i]>store[x][y]){//向左走i步
                dp[x][y]=max(dp[x][y],dfs(x,y-i)+store[x][y]);
            }
            if(y+i<n&&store[x][y+i]>store[x][y]){//向右走i步
                dp[x][y]=max(dp[x][y],dfs(x,y+i)+store[x][y]);
            }
        }
        return dp[x][y];
    }
};
int main()
{
    IOS;
    ifstream infile;
    infile.open("b.txt");
    int n,k;
    while(infile>>n>>k){
        cout<<n<<" "<<k<<endl;
        if(n==-1&&k==-1)break;
        ac::n=n;ac::k=k;
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                infile>>ac::store[i][j];
                cout<<ac::store[i][j]<<" ";
            }
            cout<<endl;
        }
        ac::init();
        cout<<ac::dfs(0,0)<<endl;
    }    
    infile.close();
    getchar();
    getchar();
    return 0;
}
